/***************************************************************************//**
 * @file CGI/DataTypeHazeScene.cpp
 * @author  Marek M. Cel <marekcel@mscsim.org>
 *
 * @section LICENSE
 *
 * Copyright (C) 2014 Marek M. Cel
 *
 * This file is part of MScSim. You can redistribute and modify it under
 * the terms of GNU General Public License as published by the Free Software
 * Foundation; either version 3 of the License, or (at your option) any later
 * version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
 * for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.
 * 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * Further information about the GNU General Public License can also be found
 * on the world wide web at http://www.gnu.org.
 ******************************************************************************/
#ifndef DATA_TYPE_HAZE_SCENE_CPP
#define DATA_TYPE_HAZE_SCENE_CPP
#endif

////////////////////////////////////////////////////////////////////////////////

#include <iostream>

#include <osg/Fog>

#include <CGI/DataTypeHazeScene.h>

////////////////////////////////////////////////////////////////////////////////

using namespace std;

////////////////////////////////////////////////////////////////////////////////

DataTypeHazeScene::DataTypeHazeScene( osg::Group *group )
{
#   ifndef MSCSIM_OSGEPHEMERIS
    m_elevation = 0.0;
#   endif

    m_fogColor      = osg::Vec4d( 0.0, 0.0, 0.0, 1.0 );
    m_fogColor_Prev = osg::Vec4d( 0.0, 0.0, 0.0, 1.0 );

#   ifndef MSCSIM_OSGEPHEMERIS
    m_fogColors.clear();

    m_fogColors.push_back( osg::Vec4d(  14.0/255.0,  16.0/255.0,  25.0/255.0, 0.0 ) ); // 05
    m_fogColors.push_back( osg::Vec4d(  28.0/255.0,  30.0/255.0,  40.0/255.0, 0.0 ) ); // 10
    m_fogColors.push_back( osg::Vec4d(  41.0/255.0,  44.0/255.0,  54.0/255.0, 0.0 ) ); // 15
    m_fogColors.push_back( osg::Vec4d(  55.0/255.0,  59.0/255.0,  69.0/255.0, 0.0 ) ); // 20
    m_fogColors.push_back( osg::Vec4d(  69.0/255.0,  73.0/255.0,  83.0/255.0, 0.0 ) ); // 25
    m_fogColors.push_back( osg::Vec4d(  83.0/255.0,  87.0/255.0,  98.0/255.0, 0.0 ) ); // 30
    m_fogColors.push_back( osg::Vec4d(  97.0/255.0, 102.0/255.0, 112.0/255.0, 0.0 ) ); // 35
    m_fogColors.push_back( osg::Vec4d( 110.0/255.0, 116.0/255.0, 127.0/255.0, 0.0 ) ); // 40
    m_fogColors.push_back( osg::Vec4d( 124.0/255.0, 130.0/255.0, 141.0/255.0, 0.0 ) ); // 45
    m_fogColors.push_back( osg::Vec4d( 138.0/255.0, 145.0/255.0, 156.0/255.0, 0.0 ) ); // 50
    m_fogColors.push_back( osg::Vec4d( 147.0/255.0, 153.0/255.0, 165.0/255.0, 0.0 ) ); // 55
    m_fogColors.push_back( osg::Vec4d( 156.0/255.0, 162.0/255.0, 174.0/255.0, 0.0 ) ); // 60
    m_fogColors.push_back( osg::Vec4d( 165.0/255.0, 170.0/255.0, 184.0/255.0, 0.0 ) ); // 65
    m_fogColors.push_back( osg::Vec4d( 174.0/255.0, 179.0/255.0, 193.0/255.0, 0.0 ) ); // 70
    m_fogColors.push_back( osg::Vec4d( 183.0/255.0, 187.0/255.0, 202.0/255.0, 0.0 ) ); // 75
    m_fogColors.push_back( osg::Vec4d( 193.0/255.0, 196.0/255.0, 212.0/255.0, 0.0 ) ); // 80
    m_fogColors.push_back( osg::Vec4d( 202.0/255.0, 204.0/255.0, 221.0/255.0, 0.0 ) ); // 85
    m_fogColors.push_back( osg::Vec4d( 211.0/255.0, 213.0/255.0, 231.0/255.0, 0.0 ) ); // 90
    m_fogColors.push_back( osg::Vec4d( 220.0/255.0, 221.0/255.0, 240.0/255.0, 0.0 ) ); // 95
#   endif

    m_hazeSceneGroup = group;
}

////////////////////////////////////////////////////////////////////////////////

void DataTypeHazeScene::update()
{
    if ( m_hazeSceneGroup.valid() )
    {
#       ifndef MSCSIM_HAZE_DISABLED
        m_fogColor = getFogColor();

        if ( m_fogColor_Prev != m_fogColor )
        {
            m_hazeSceneGroup->setStateSet( setFogState().get() );
        }
#       endif
    }
    else
    {
        cerr << "ERROR! Wrong node pointer." << endl;
    }

    m_fogColor_Prev = m_fogColor;
}

////////////////////////////////////////////////////////////////////////////////

osg::Vec4d DataTypeHazeScene::getFogColor()
{
#   ifndef MSCSIM_OSGEPHEMERIS
    double elevation_deg = osg::RadiansToDegrees( m_elevation );

    if ( elevation_deg >= -12.0 && elevation_deg < 12.0 )
    {
        int colorId = floor( 19.0 * ( elevation_deg + 12.0 ) / 24.0 );

        if ( colorId <  0 ) colorId =  0;
        if ( colorId > 18 ) colorId = 18;

        return m_fogColors.at( colorId );
    }
    else
    {
        if ( elevation_deg < -12.0 )
        {
            return osg::Vec4d(   1.0/255.0,   2.0/255.0,  11.0/255.0, 0.0 );
        }
        else
        {
            return osg::Vec4d( 230.0/255.0, 230.0/255.0, 250.0/255.0, 0.0 );
        }
    }
#   else
#   endif

    return osg::Vec4d( 0.0, 0.0, 0.0, 1.0 );
}

////////////////////////////////////////////////////////////////////////////////

osg::ref_ptr<osg::StateSet> DataTypeHazeScene::setFogState()
{
    osg::ref_ptr<osg::Fog> fog = new osg::Fog();

    fog->setMode( osg::Fog::LINEAR );
    fog->setDensity( 0.5f );
    fog->setColor( m_fogColor );
    fog->setStart( 0.0f );
    //fog->setEnd( 150000.0f );
    fog->setEnd( 100000.0f );
    //fog->setEnd( 5000.0f );

    osg::ref_ptr< osg::StateSet> fogState = new osg::StateSet();

    fogState->setAttributeAndModes( fog.get(), osg::StateAttribute::ON );
    fogState->setMode( GL_FOG, osg::StateAttribute::ON );

    return fogState;
}

